package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Auther: Tangguo
 * @Date: 2023-06-20  15:52
 */

@Service
@Slf4j
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CacheService cacheService;

    /**
     * 查询列表
     * @param dto :
     * @return ResponseResult
     */
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //1.参数校验
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.构建分页查询
        Page page=new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmSensitive> wrapper = Wrappers.<WmSensitive>lambdaQuery();

        //按照敏感词名称模糊查询
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(WmSensitive::getSensitives,dto.getName());
        }

        //按照创建时间倒序查询
        wrapper.orderByDesc(WmSensitive::getCreatedTime);

        //3.查询数据库
        page(page,wrapper);

        //4.封装返回结果
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 新增敏感词
     * @param wmSensitive :
     * @return ResponseResult
     */
    @Override
    public ResponseResult insert(WmSensitive wmSensitive) {
        //1.参数校验
        if(wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.数据处理
        //已存在的敏感词不能新增
        WmSensitive sensitive = this.getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if(sensitive!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词："+wmSensitive.getSensitives()+" 已存在！！");
        }
        //3.业务操作
        //更新创建时间
        wmSensitive.setCreatedTime(new Date());
        //保存敏感词到数据库
        this.save(wmSensitive);

        //清理redis中的敏感词缓存
        clearCacheSensitive();
        //4.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     * @param wmSensitive :
     * @return ResponseResult
     */
    @Override
    public ResponseResult update(WmSensitive wmSensitive) {
        //1.参数校验
        if(wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.数据处理
        //2.判断修改后的敏感词是否已存在
        int count = count(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词：" + wmSensitive.getSensitives() + " 已存在!!");
        }
        //3.业务操作
        this.updateById(wmSensitive);

        //清理redis中的敏感词缓存
        clearCacheSensitive();

        //4.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     * @param id :
     * @return ResponseResult
     */
    @Override
    public ResponseResult delete(Integer id) {
        //参数校验
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查传过来的id是否存在
        WmSensitive sensitive = getById(id);
        if(sensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id删除
        removeById(id);

        //清理redis中的敏感词缓存
        clearCacheSensitive();
        //返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 删除redis中的敏感词
     */
    private void clearCacheSensitive() {
        cacheService.delete("SensitiveKeys");
        System.out.println("清理redis中的敏感词缓存OK");
    }
}
